package com.xinxing.city.recovery.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xinxing.city.recovery.Constant;
import com.xinxing.city.recovery.R;
import com.xinxing.city.recovery.adapter.CameraAdapter;
import com.xinxing.city.recovery.databinding.ActivitySetDeviceBinding;
import com.xinxing.city.recovery.entitys.Bucket;
import com.xinxing.city.recovery.entitys.BucketType;
import com.xinxing.city.recovery.entitys.Cabinet;
import com.xinxing.city.recovery.entitys.Devena;
import com.xinxing.city.recovery.utils.MyDialog;
import com.xinxing.city.recovery.utils.SharedPreferencesUtils;

import java.lang.reflect.Type;
import java.util.List;

public class SetDeviceActivity extends AppCompatActivity {
    private ActivitySetDeviceBinding binding;
    private Devena devena;
    private Cabinet cabinet;
    private List<BucketType> buckets = null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_set_device);
        //监听点击
        binding.t1chsC.setOnClickListener(view1 -> {
            CameraAdapter cameraAdapter = new CameraAdapter(0);
            cameraAdapter.show(getSupportFragmentManager(),"MyDialog");
        });

        binding.t2chsC.setOnClickListener(view12 -> {
            CameraAdapter cameraAdapter = new CameraAdapter(1);
            cameraAdapter.show(getSupportFragmentManager(),"MyDialog");
        });

        binding.t3chsC.setOnClickListener(view13 -> {
            CameraAdapter cameraAdapter = new CameraAdapter(2);
            cameraAdapter.show(getSupportFragmentManager(),"MyDialog");
        });

        binding.t4chsC.setOnClickListener(view14 -> {
            CameraAdapter cameraAdapter = new CameraAdapter(3);
            cameraAdapter.show(getSupportFragmentManager(),"MyDialog");
        });

        binding.t5chsC.setOnClickListener(view15 -> {
            CameraAdapter cameraAdapter = new CameraAdapter(4);
            cameraAdapter.show(getSupportFragmentManager(),"MyDialog");
        });

        binding.t6chsC.setOnClickListener(view16 -> {
            CameraAdapter cameraAdapter = new CameraAdapter(5);
            cameraAdapter.show(getSupportFragmentManager(),"MyDialog");
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        init();
        binding.setDevena(devena);
    }

    private void init() {
        if(Constant.screenWidth==800){
            binding.tong2.setVisibility(View.GONE);
            binding.tong3.setVisibility(View.GONE);
            binding.tong4.setVisibility(View.GONE);
        }
        String devenaStr = (String) SharedPreferencesUtils.getParam(this, "set_devena", "");
        Gson gson = new Gson();
        if (!devenaStr.equals("")) {
            Type dev = new TypeToken<Devena>() {
            }.getType();
            devena = gson.fromJson(devenaStr, dev);
        } else {
            devena = new Devena();
        }
        /*柜子*/
        String cabinetStr = (String) SharedPreferencesUtils.getParam(this, "cabinet", "");

        if (!cabinetStr.equals("")) {
            Type dev = new TypeToken<Cabinet>() {
            }.getType();
            cabinet = gson.fromJson(cabinetStr, dev);
            imageSet(cabinet.getCode(), "g");
        }
        /*桶*/
        String bucketStr = (String) SharedPreferencesUtils.getParam(this, "bucket", "");

        int count = 0;
        if (!bucketStr.equals("")) {
            Type dev = new TypeToken<List<BucketType>>() {}.getType();
            buckets = gson.fromJson(bucketStr, dev);
            count = buckets.size();
        }
        for (int i = 1; i <= 6; i++) {
            if(count>=i && buckets!=null){
                Log.e("TTT","count "+count+"    i "+i);
                BucketType bucket = buckets.get(i-1);
                imageSet(bucket.getCode(), "t" + i);
            }else{
                imageSet("", "t" + i);
            }
            //电子锁
            imageSet("", "t" + i + "dzs");
            //窗口锁
            imageSet("", "t" + i + "cks");
            //窗户锁
            imageSet("", "t" + i + "chs");
            //满溢
            imageSet("", "t" + i + "my");
        }
    }

    /**
     * 跳转柜子
     */
    public void tzSetCabinet(View view) {
        Intent intent = new Intent(this, SetCabinetActivity.class);
        if(cabinet!=null) {
            intent.putExtra("code", cabinet.getCode());
        }else {
            intent.putExtra("code", "");
        }
        startActivity(intent);
    }

    /**
     * 跳转环境摄像头
     */
    public void tzSetDeviceHj(View view) {
        //判断柜子是否存在
        if(cabinet!=null) {
            if (cabinet.getCode().equals("")) {
                MyDialog myDialog = MyDialog.newInstance("提示", "请先注册回收柜!", "返回", "");
                myDialog.show(getSupportFragmentManager(), "MyDialog");
                myDialog.setMyDialogListener(new MyDialog.MyDialogListener() {
                    @Override
                    public void onPositiveClick() {
                        myDialog.dismiss();
                    }

                    @Override
                    public void onNegativeClick() {

                    }
                });
            } else {
                Intent intent = new Intent(this, SetDeviceHkActivity.class);
                startActivity(intent);
            }
        }
    }

    /**
     * 跳转桶
     */
    public void tzSetBucket(View view) {

        //判断柜子是否存在
        if(cabinet!=null) {
            Integer bucketNum = Integer.parseInt(view.getTag().toString());
            Intent intent = new Intent(this, SetBucketActivity.class);
            intent.putExtra("bucketNum", bucketNum);
            intent.putExtra("cabinetCode",cabinet.getCode());
            intent.putExtra("cabinetName",cabinet.getName());
            startActivity(intent);
        }else{
            MyDialog myDialog = MyDialog.newInstance("提示", "请先注册回收柜!", "返回", "");
            myDialog.show(getSupportFragmentManager(), "MyDialog");
            myDialog.setMyDialogListener(new MyDialog.MyDialogListener() {
                @Override
                public void onPositiveClick() {
                    myDialog.dismiss();
                }

                @Override
                public void onNegativeClick() {

                }
            });
        }
    }

    public void tzSetDzs(View view) {
        //判断桶子是否存在
        String num = view.getTag().toString();
        if(buckets!=null&&buckets.size()>=Integer.valueOf(num)){
            Integer dszNum = Integer.parseInt(view.getTag().toString());
            Intent intent = new Intent(this, SetBucketDzsActivity.class);
            intent.putExtra("dszNum", dszNum);
            startActivity(intent);
        }else{
            MyDialog myDialog = MyDialog.newInstance("提示", "请先注册对应桶!", "返回", "");
            myDialog.show(getSupportFragmentManager(), "MyDialog");
            myDialog.setMyDialogListener(new MyDialog.MyDialogListener() {
                @Override
                public void onPositiveClick() {
                    myDialog.dismiss();
                }

                @Override
                public void onNegativeClick() {

                }
            });
        }
    }
    /**
     * 跳转判定
     */
    public void tzTips(View view) {
        MyDialog myDialog = MyDialog.newInstance("提示", "功能正在建设中...", "确定", "");
        myDialog.show(getSupportFragmentManager(), "MyDialog");
    }

    /**
     * 图片设置
     */
    private void imageSet(String code, String idstr) {
        ImageView imageView = findViewById(getResources().getIdentifier(idstr, "id", getPackageName()));
        if (code != "" || !code.isEmpty()) {
            AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 1.0f);
            alphaAnimation.setDuration(1000); // 设置动画持续时间
            alphaAnimation.setRepeatCount(Animation.INFINITE); // 设置动画重复次数（无限循环）
            imageView.startAnimation(alphaAnimation);
        } else {
            AlphaAnimation alphaAnimation = new AlphaAnimation(0.4f, 0.4f);
            alphaAnimation.setDuration(1000); // 设置动画持续时间
            alphaAnimation.setRepeatCount(Animation.INFINITE); // 设置动画重复次数（无限循环）
            imageView.startAnimation(alphaAnimation);
        }
    }
}
